﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using BEGDC.CC.BL.Common;
using BEGDC.CC.BL.Workflow;
using BEGDC.CC.Entity;
using BEGDC.Utility.Castle;

namespace BEGDC.CC.Web
{
    /// <summary>
    /// Summary description for ShowTypeForm.
    /// </summary>
    public class ShowFNCCofigurationForm : System.Web.UI.Page
    {
        protected System.Web.UI.WebControls.Label lbName;
        protected System.Web.UI.WebControls.TextBox txtName;
        protected System.Web.UI.WebControls.TextBox txtDescription;
        protected System.Web.UI.WebControls.Button btnSave;
        protected System.Web.UI.WebControls.Label lbDescription;
        protected System.Web.UI.WebControls.DropDownList ddlDept;
        protected System.Web.UI.WebControls.DataGrid dgUserDept;
        protected System.Web.UI.WebControls.Button btnClose;
        private int UserRoleID;

        private void Page_Load(object sender, System.EventArgs e)
        {
            // Put user code to initialize the page here
            string id = Request.QueryString["id"];
            this.UserRoleID = Convert.ToInt32(id);

            if (!Page.IsPostBack)
            {
                InitializeGlobalization();
                BindDept();
                BindUserRole(this.UserRoleID);

                if (((BEGDC.CC.BL.Workflow.CCPrincipal)Page.User).Roles.IndexOf("BusinessAdmin") > -1 && ((BEGDC.CC.BL.Workflow.CCPrincipal)Page.User).Roles.IndexOf("RoleAdmin") == -1)
                {
                    this.btnSave.Visible = false;
                }

            }
        }

        private void BindDept()
        {
            BEGDC.CC.BL.Common.Query.IQuery query = AppContainer.Instance[ServiceConst.QueryService] as BEGDC.CC.BL.Common.Query.IQuery;
            DataTable dept = query.GetAllDepartment();
            dept.DefaultView.Sort = "DepartmentCode ASC";
            this.ddlDept.DataSource = dept;
            this.ddlDept.DataTextField = "DepartmentCode";
            this.ddlDept.DataValueField = "DepartmentID";
            this.ddlDept.DataBind();
        }
        private void BindUserRole(int id)
        {
            BEGDC.CC.BL.UserManagement.IUser iuser = AppContainer.Instance[ServiceConst.UserService] as BEGDC.CC.BL.UserManagement.IUser;
            DataTable dt = iuser.GetFNCDeptByUserRoleID(id);
            this.dgUserDept.DataSource = dt.DefaultView;
            this.dgUserDept.DataBind();

        }

        private void InitializeGlobalization()
        {

            //this.lblTitle.Text = "CAT Director";
            this.lbName.Text = "Department(部门):";
        }
        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.btnSave.Click += new EventHandler(btnSave_Click);
            this.btnClose.Click += new EventHandler(btnClose_Click);
            this.dgUserDept.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.dgUserDept_ItemDataBound);
            this.dgUserDept.ItemCommand += new DataGridCommandEventHandler(dgUserDept_ItemCommand);
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion
        void btnSave_Click(object sender, EventArgs e)
        {
            BEGDC.CC.BL.UserManagement.IUser iuser = AppContainer.Instance[ServiceConst.UserService] as BEGDC.CC.BL.UserManagement.IUser;

            int deptID = Convert.ToInt32(this.ddlDept.SelectedValue);
            foreach (DataGridItem di in this.dgUserDept.Items)
            {
                if (di.Cells[1].Text == deptID.ToString())
                {
                    Page.RegisterClientScriptBlock("", "<script language='javascript'>alert('This department is alreay assigned!');</script>");
                    return;
                }
            }


            if (iuser.SaveFNCDept(this.UserRoleID, deptID))
            {
                IUtility utility = AppContainer.Instance[ServiceConst.UtilityService] as IUtility;
                this.BindUserRole(this.UserRoleID);
                //Page.RegisterClientScriptBlock("","<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CM001 + "');returnValue='Type';window.close();</script>");
                CCIdentity id = this.User.Identity as CCIdentity;
                BEGDC.CC.Entity.OperationLog log = new BEGDC.CC.Entity.OperationLog();
                log.Module = "FNC For Department Configuration";
                Employee emp = new Employee();
                emp.Id = id.UserInfo.Id;
                log.Employee = emp;
                log.Time = System.DateTime.Now;
                log.Description = "Update FNC for department property of Employee .";
                utility.SaveOperationLog(log);
            }
            else
                Page.RegisterClientScriptBlock("", "<script>alert('" + BEGDC.CC.Resource.SystemMessage.CR002 + "');</script>");

        }

        void btnClose_Click(object sender, EventArgs e)
        {
            Page.RegisterClientScriptBlock("", "<script>returnValue='';window.close();</script>");
        }

        private void dgUserDept_ItemDataBound(object source, System.Web.UI.WebControls.DataGridItemEventArgs e)
        {

            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                string userroleid = e.Item.Cells[0].Text;
                ImageButton iblegal = (ImageButton)e.Item.FindControl("imageLegal");
                iblegal.Attributes.Add("onclick", "javascript:OnDelete();");
            }
        }

        private void dgUserDept_ItemCommand(object sender, DataGridCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                BEGDC.CC.BL.UserManagement.IUser iuser = AppContainer.Instance[ServiceConst.UserService] as BEGDC.CC.BL.UserManagement.IUser;
                if (iuser.RemoveFNCDept(Convert.ToInt32(e.Item.Cells[0].Text), Convert.ToInt32(e.Item.Cells[1].Text)))
                {
                    this.BindUserRole(this.UserRoleID);
                    //write log
                    IUtility utility = AppContainer.Instance[ServiceConst.UtilityService] as IUtility;
                    CCIdentity id = this.User.Identity as CCIdentity;
                    BEGDC.CC.Entity.OperationLog log = new BEGDC.CC.Entity.OperationLog();
                    log.Module = "FNC For Department Configuration";
                    Employee emp = new Employee();
                    emp.Id = id.UserInfo.Id;
                    log.Employee = emp;
                    log.Time = System.DateTime.Now;
                    log.Description = "Delete FNC for department property of Employee " + e.Item.Cells[3].Text;
                    utility.SaveOperationLog(log);
                }
            }
        }
    }
}
